<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Broadcast extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $this->load->model('luckydog/Broadcast_model', 'broadcast');
         $this->load->model('luckydog/Admin_user_model', 'admin_user');
	}

/**
 * 新闻列表
 * @return [type] [description]
 */
    public function index(){
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $_SESSION['v_menu'] = 'broadcast';
        $limit = 20;
        $total_rows = $this->broadcast->getBroadcastCnt($per_page,$limit);
        $base_url = '/luckydog/broadcast/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $broadcastList = $this->broadcast->getBroadcastList($per_page, $limit);
        $data = array(
            'broadcastList'=>$broadcastList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            );
        $this->load->view("/luckydog/broadcast/index", $data);
    }

    public function textures()
    {
        $id = $this->input->get("id",true)?$this->input->get("id",true):"";
        if ($login_status === FALSE){
            header("Location:login");
        }
        $_SESSION['v_menu'] = 'broadcast';
        $data = array();
        if(!empty($id)){
            $broadcastInfo = $this->broadcast->getBroadcastInfobyID($id);
            $data["broadcastInfo"] = $broadcastInfo;
        }
    	$this->load->view("/luckydog/broadcast/textures", $data);
    }


    //编辑公告
    public function editBroadcast(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $content = $this->input->post("contents",true) ? $this->input->post("contents",true) : "";
        $title = $this->input->post("title",true) ? $this->input->post("title",true) : "";
        $power = json_decode($_SESSION['adminUserInfo']['power'],1);
        $isAdministrator = array_key_exists("System", $power)?1:0;
        $data = array(
            "content"=>$content,
            "title"=>$title,
            "isAdministrator"=>$isAdministrator,
        );
        if(empty($id)){
            $data['publishtime']=time();
        }
        $res = $this->broadcast->editBroadcast($id,$data);
        echo $res;
    }

    public function do_publish(){
         $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
         $res= 0;
         if($id){
            $this->broadcast->editBroadcast($id,array('publishtime'=>time(),"isbroadcast"=>1));
            $data_str = "";
            $all_admin_user = $this->admin_user->getAllUser();
            $publishtime = time();
            foreach ($all_admin_user as $key => $value) {
                $data_str .= "(".$value['id'].",".$id.",".$publishtime."),";
            }
            $data_str = substr($data_str,0,-1);
            $res = $this->broadcast->do_publish($data_str);
         }
        echo $res;
    }


    //编辑上线下，删除状态
    public function delBroadcast(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $res =$this->broadcast->delBroadcast($id);
        echo $res;
    }


    public function mybroadcast()
    {
        $userid = $_SESSION['adminUserInfo']['id'];
	    $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $limit = 20;
        $total_rows = $this->broadcast->getBroadcastCnt($per_page,$limit);
        $base_url = '/luckydog/broadcast/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);

        $broadcastList = $this->broadcast->getBroadcastList($per_page, $limit);
        $data = array(
            'broadcastList'=>$broadcastList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
            );
	    $this->load->view("/luckydog/broadcast/mybroadcast", $data);
    }

    public function broadcastDetail(){
        $id = $this->input->get("id",true) ? $this->input->get("id",true) : 0;
        $broadcastDetail = $this->broadcast->getBroadcastDetail($id);
        $setread = $this->broadcast->setRead($id);
        $data = array('broadcastDetail'=>$broadcastDetail);
        $this->load->view("/luckydog/broadcast/broadcastdetail", $data);   
    }


}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/index.php */
